package com.peanut.controller;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.peanut.domain.po.TabSpecPO;
import com.peanut.dto.TabSpecInfoDTO;
import com.peanut.em.ResultEnum;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import com.peanut.common.Result;
import com.peanut.domain.bo.TabSpecBO;
import com.peanut.dto.TabSpecDTO;
import com.peanut.service.TabSpecService;
import io.swagger.annotations.Api;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("spec")
@Api("规格管理Api")
public class SpecController {

    @Resource
    private TabSpecService specService;

    @Resource
    private RestTemplate restTemplate;

    /**
     * 添加新规格
     * @param dto : 规格实体类中的数据
     * @return
     */
    @PostMapping
    @ApiOperation("添加新规格")
    @ApiImplicitParam(name = "dto",value = "规格实体类",required = true,paramType ="body")
    public Result addSpec(@RequestBody TabSpecDTO dto){
        try {
            TabSpecBO bo = new TabSpecBO();
            BeanUtils.copyProperties(dto,bo);
            int i = specService.addSpec(bo);
            return Result.OK().setData(i);
        }catch (Exception e){
            e.printStackTrace();
            return Result.ERROR();
        }
    }

    /**
     * 查询所有规格，并分页
     * @param pageNumber : 当前页面
     * @param pageSize : 每页条数
     * @return
     */
    @PostMapping("findAll")
    @ApiOperation("查询所有规格")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNumber",value = "当前页面",required = true,paramType ="query"),
            @ApiImplicitParam(name = "pageSize",value = "每页条数",required = true,paramType ="query")
    })
    public Result findAll(@RequestParam(value = "pageNumber",defaultValue = "1")Integer pageNumber,
                          @RequestParam(value = "pageSize",defaultValue = "3")Integer pageSize){
        try {
            Page<TabSpecPO> page = specService.findAll(pageNumber,pageSize);
            return Result.OK().setData(page);
        }catch (Exception e){
            e.printStackTrace();
            return Result.ERROR();
        }
    }

    /**
     * 修改规格数据
     * @param dto : 规格实体类中的数据
     * @return
     */
    @PutMapping
    @ApiOperation("修改规格数据")
    @ApiImplicitParam(name = "dto",value = "规格实体类",required = true,paramType ="body")
    public Result updateSpec(@RequestBody TabSpecDTO dto){
        try {
            TabSpecBO bo = new TabSpecBO();
            BeanUtils.copyProperties(dto,bo);
            int i = specService.updateSpec(bo);
            return Result.OK().setData(i);
        }catch (Exception e){
            e.printStackTrace();
            return Result.ERROR();
        }
    }

    @GetMapping("getSpec")
    @ApiOperation("查询数据")
    public Result<TabSpecDTO> getSpec(){
        try {
            List<TabSpecPO> list = specService.getSpec();
            List<TabSpecDTO> list1 = new ArrayList<>();
            for (TabSpecPO tabSpecPO : list) {
                TabSpecDTO dto = new TabSpecDTO();
                BeanUtils.copyProperties(tabSpecPO,dto);
                list1.add(dto);
            }
            return Result.OK().setData(list1);
        }catch (Exception e){
            e.printStackTrace();
            return Result.ERROR();
        }
    }

    @DeleteMapping("/{specId}")
    public Result deleteSpec(@PathVariable Long specId){
        try {
            ResponseEntity<Result> entity = restTemplate.getForEntity("http://127.0.0.1:8079/info/selectById?specId=" + specId, Result.class);
            if (entity.getStatusCodeValue() == 200){
                Result body = entity.getBody();
                Integer code = body.getCode();
                if (code == 10000){
                    String json = JSONUtil.toJsonStr(body.getData());
                    JSONArray objects = JSONUtil.parseArray(json);
                    List<TabSpecInfoDTO> specInfoDTO = JSONUtil.toList(objects, TabSpecInfoDTO.class);
                    if (specInfoDTO == null){
                        int i = specService.deleteSpec(specId);
                        return Result.OK().setData(i);
                    }else{
                        return Result.R(ResultEnum.NO_NULL).setData(specInfoDTO);
                    }
                }
            }
            return Result.ERROR();
        }catch (Exception e){
            e.printStackTrace();
            return Result.ERROR();
        }
    }

}
